package com.kaizeli.website.service.impl;

import com.kaizeli.website.mapper.AttendanceMapper;
import com.kaizeli.website.pojo.entity.AttendanceDO;
import com.kaizeli.website.pojo.vo.AttendanceVO;
import com.kaizeli.website.service.AttendanceService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 考勤服务实现类
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class AttendanceServiceImpl implements AttendanceService {
    
    private final AttendanceMapper attendanceMapper;
    
    @Override
    public Long save(AttendanceDO attendanceDO) {
        log.info("新增考勤记录，员工ID：{}，日期：{}", attendanceDO.getEmployeeId(), attendanceDO.getAttendanceDate());
        attendanceMapper.insert(attendanceDO);
        return attendanceDO.getId();
    }
    
    @Override
    public Boolean update(AttendanceDO attendanceDO) {
        log.info("更新考勤记录，ID：{}", attendanceDO.getId());
        return attendanceMapper.updateById(attendanceDO) > 0;
    }
    
    @Override
    public AttendanceVO getById(Long id) {
        log.info("根据ID查询考勤记录，ID：{}", id);
        AttendanceDO attendanceDO = attendanceMapper.selectById(id);
        if (attendanceDO == null) {
            return null;
        }
        return convertToVO(attendanceDO);
    }
    
    @Override
    public Boolean deleteById(Long id) {
        log.info("根据ID删除考勤记录，ID：{}", id);
        return attendanceMapper.deleteById(id) > 0;
    }
    
    @Override
    public List<AttendanceVO> listByEmployee(Long employeeId) {
        log.info("根据员工ID查询考勤记录，员工ID：{}", employeeId);
        List<AttendanceDO> list = attendanceMapper.selectList(
            new com.baomidou.mybatisplus.core.conditions.query.QueryWrapper<AttendanceDO>()
                .eq("employee_id", employeeId)
                .orderByDesc("attendance_date")
        );
        return list.stream().map(this::convertToVO).collect(Collectors.toList());
    }
    
    @Override
    public List<AttendanceVO> listByProject(Long projectId) {
        log.info("根据项目ID查询考勤记录，项目ID：{}", projectId);
        List<AttendanceDO> list = attendanceMapper.selectList(
            new com.baomidou.mybatisplus.core.conditions.query.QueryWrapper<AttendanceDO>()
                .eq("project_id", projectId)
                .orderByDesc("attendance_date")
        );
        return list.stream().map(this::convertToVO).collect(Collectors.toList());
    }
    
    @Override
    public List<AttendanceVO> listByEmployeeAndDateRange(Long employeeId, LocalDate startDate, LocalDate endDate) {
        log.info("根据员工ID和日期范围查询考勤记录，员工ID：{}，开始日期：{}，结束日期：{}", employeeId, startDate, endDate);
        List<AttendanceDO> list = attendanceMapper.selectByEmployeeAndDateRange(employeeId, startDate, endDate);
        return list.stream().map(this::convertToVO).collect(Collectors.toList());
    }
    
    @Override
    public List<AttendanceVO> listByProjectAndDateRange(Long projectId, LocalDate startDate, LocalDate endDate) {
        log.info("根据项目ID和日期范围查询考勤记录，项目ID：{}，开始日期：{}，结束日期：{}", projectId, startDate, endDate);
        List<AttendanceDO> list = attendanceMapper.selectByProjectAndDateRange(projectId, startDate, endDate);
        return list.stream().map(this::convertToVO).collect(Collectors.toList());
    }
    
    @Override
    public Double sumWorkHoursByEmployee(Long employeeId, LocalDate startDate, LocalDate endDate) {
        log.info("统计员工工作时长，员工ID：{}，开始日期：{}，结束日期：{}", employeeId, startDate, endDate);
        Double total = attendanceMapper.sumWorkHoursByEmployee(employeeId, startDate, endDate);
        return total != null ? total : 0.0;
    }
    
    /**
     * 转换为VO
     */
    private AttendanceVO convertToVO(AttendanceDO attendanceDO) {
        AttendanceVO vo = new AttendanceVO();
        vo.setId(attendanceDO.getId());
        vo.setEmployeeId(attendanceDO.getEmployeeId());
        vo.setEmployeeName(attendanceDO.getEmployeeName());
        vo.setEmployeeCode(attendanceDO.getEmployeeCode());
        vo.setProjectId(attendanceDO.getProjectId());
        vo.setProjectCode(attendanceDO.getProjectCode());
        vo.setProjectName(attendanceDO.getProjectName());
        vo.setAttendanceDate(attendanceDO.getAttendanceDate());
        vo.setCheckInTime(attendanceDO.getCheckInTime());
        vo.setCheckOutTime(attendanceDO.getCheckOutTime());
        vo.setWorkHours(attendanceDO.getWorkHours());
        vo.setAttendanceStatus(attendanceDO.getAttendanceStatus());
        vo.setLeaveType(attendanceDO.getLeaveType());
        vo.setOvertimeHours(attendanceDO.getOvertimeHours());
        vo.setRemark(attendanceDO.getRemark());
        vo.setStatus(attendanceDO.getStatus());
        vo.setApproverId(attendanceDO.getApproverId());
        vo.setApproverName(attendanceDO.getApproverName());
        return vo;
    }
}

